<?php
function random_float ($min,$max) {
	return ($min+lcg_value()*(abs($max-$min)));
}
// Configuration
//http://www.franzone.com/2008/05/20/how-do-i-get-meta-data-from-mysql-using-php/

$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'cc_v2';
$countries = array("Australia", "Bangladesh", "England", "India", "New Zealand", "Pakistan", "South Africa", "Sri Lanka", "West Indies", "Zimbabwe");
$playerTypes = array("Batsman","Bowler", "AllRounder","WicketKeeper");
$playerNumbers = array(20,15,10,5);
$playerTypeNumbers = array();
$playerTypeNumbers["Batsman"]=20;
$playerTypeNumbers["Bowler"]=15;
$playerTypeNumbers["AllRounder"]=10;
$playerTypeNumbers["WicketKeeper"]=5;
$battingStyle=array("LH","RH");
$bowlingStyle=array("RF","LF","RFM","LFM","RLS","LLS","ROS","LOS");


// Connect to the MySQL database
$conn = mysql_connect($host, $user, $pass) or die("Could not connect to database\n");
mysql_select_db($database, $conn) or die("Could not connect to $database\n");


$tableName="player";

$cols = mysql_query("SHOW COLUMNS FROM $tableName", $conn);
if ($cols) {
	$output = array();
	while ($col = mysql_fetch_assoc($cols)) {
		$output[$col['Field']] = $col['Type'];
	}
	mysql_free_result($cols);
}
echo "delete FROM $tableName;<br>";
$strQuery = "";
foreach ( $countries as $country ) {
	foreach ( $playerTypeNumbers as $key => $value ) {
		for ( $index = 0; $index < $value; $index++ ) {
			$strQuery = "Insert into $tableName (";
			foreach ($output as $field => $type) {
				if(strcasecmp($field,"id")!=0){
					$strQuery = $strQuery."$field, ";
				}
			}
			$strQuery = substr($strQuery , 0, -2);
			$strQuery = $strQuery.") values (";
			foreach ($output as $field => $type) {
				if(strcasecmp($field,"id")!=0){
					if(strcasecmp($field,"battingstyle")==0 || strcasecmp($field,"fieldingstyle")==0){
						$number= mt_rand(0,1);
						$strQuery =$strQuery . " '$battingStyle[$number]' ,";
					}else if(strcasecmp($field,"bowlingstyle")==0){
						$number= mt_rand(0,8);
						$strQuery =$strQuery . " '$bowlingStyle[$number]' ,";
					}else if(strpos($field, 'Type')>0){
						$strQuery = $strQuery."'$key', ";
					} else if (strcasecmp($field,"country")==0) {
						$strQuery = $strQuery."'$country', ";
					} else if (strpos($type, 'loat')==1) {
						$number= round(random_float(0,10),2);
						$strQuery =$strQuery. " '$number' ,";
					} else if (strpos($type, 'time')==4) {
						if((strpos($field, 'of_birth')==5)){
							$time = mt_rand( time()-(15000 * 24 * 60 * 60), time()-(6600 * 24 * 60 * 60) );
						}else{
							$time = mt_rand( time()-(100 * 24 * 60 * 60), time() );	
						}
						$dt = date("Y-m-d H:i:s", $time);
						$strQuery= $strQuery. " '".$dt. "' ,";
					}else if (strpos($type, 'nt')==1) {
						$number= mt_rand(0,1000);
						$strQuery =$strQuery . " '$number' ,";
					} else if (strpos($type, 'char')>0) {
						$number= mt_rand(0,1000);
						$strQuery =$strQuery . " '$field$number' ,";
					} 
				}
			}
			$strQuery = substr($strQuery , 0, -2);
			$strQuery = $strQuery.");";
			echo "$strQuery<br>";
		}
		
	}
}

if ($conn)
	mysql_close($conn);

?>